﻿using System;
using System.Collections.Generic;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MulagoDb.model;
using NHibernate;
using MulagoDb.model_old;

namespace MulagoDb
{
    public partial class PatientReg : UserControl
    {
        public static Patient biodata;
        public static IList<Parents> parents;
        public static IList<Addres> address; 
        public PatientReg()
        {
            InitializeComponent();
        }

        private void PatientReg_Load(object sender, EventArgs e)
        {
            DoBlabel.Visible = false;
            if (!FindPatient.patienceName.Equals(""))
            {
                clinicNumTfld.Enabled = false;
                submitBtn.Enabled = false;
                CareTakerslabel.Visible = false;

            }
            else {
                clinicNumTfld.Enabled = true;
                updatebtn.Enabled = false;
                CareTakerslabel.Visible = false;
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {

        }

        public void button1_Click(object sender, EventArgs e)
        {
            string names = firstNameTFld.Text + " " + middleNameTfld.Text + " " + surnameTfld.Text;
            string dob = DayTfld.Text + "-" + MonthTfld.Text + "-" + YearTfld.Text;
            if (radioButton2.Checked == false && radioButton1.Checked == false)
            {
                MessageBox.Show(this, " Select the Sex ");
            }

            Patient patient = new Patient();
            patient.names = names;
            patient.hospNum = hospitalNumTfld.Text;
            patient.clinicNum = clinicNumTfld.Text;
            patient.dateOfBirth = dob;
            patient.phoneNum = phoneNumTfld.Text;
            patient.sex = radioButton2.Checked ? "Male" : "Female";

            Parents father = new Parents();
            father.names = fathersNameTfld.Text;
            father.description = "father";
            father.tribe = fathersTribeTfld.Text;
            father.occupation = FatherEducation.Text;
            patient.parentses.Add(father);
            father.patient = patient;

            Parents mother = new Parents();
            mother.names = mothersNameTfld.Text;
            mother.description = "mother";
            mother.tribe = mothersTribeTfld.Text;
            mother.occupation = MotherEducation.Text;
            patient.parentses.Add(mother);
            mother.patient = patient;

            Addres patientAddr = new Addres();
            patientAddr.village = villageTFld.Text;
            patientAddr.county = countyTfld.Text;
            patientAddr.district = districtTfld.Text;
            patientAddr.telcaretaker = phoneNumTfld.Text;
            patientAddr.telself = telSelfTfld.Text;
            patientAddr.personWhoCares = getCaringPerson().ToString().Trim();
            patient.addreses.Add(patientAddr);
            patientAddr.patient = patient;
            if (PatientRegistration.addPatients(patient))
            {
                MessageBox.Show(this, "Successfully added");
            }
            else
            {
                MessageBox.Show(this, "Patient already exists");
            }

/*
            if (PatientRegistration.addPatients(names, hospitalNumTfld.Text,
                     clinicNumTfld.Text, dob, phoneNumTfld.Text, radioButton2.Checked ? "Male" : "Female", fathersNameTfld.Text, "father",
                     fathersTribeTfld.Text, FatherEducation.Text, villageTFld.Text, countyTfld.Text,
                     districtTfld.Text, phoneNumTfld.Text, telSelfTfld.Text, getCaringPerson().ToString().Trim()))
            {
                MessageBox.Show(this, "Successfully added");
            }
            else {
                MessageBox.Show(this, "Patient already exists");
            }
          */ 
           
        }

        public void pupulatePatientReg(int patientId) {
            //get this patient from db
           IList<Patient> result = PatientRegistration.persist. RetrieveEquals<Patient>("pid",patientId);
           biodata = result.ElementAt(0);
            DoBlabel.Visible = true;
            parents = biodata.parentses;
            address = biodata.addreses;
            //populate fields

            string[] splitNames = biodata.names.Split(' ');
            firstNameTFld.Text = splitNames[0];
            surnameTfld.Text = splitNames[2];
            middleNameTfld.Text = splitNames[1];
            hospitalNumTfld.Text = biodata.hospNum;
            clinicNumTfld.Text = biodata.clinicNum;
            DoBlabel.Text = biodata.dateOfBirth;
            string[] dateText = biodata.dateOfBirth.Split('-');
            try
            {
                DayTfld.Text = dateText[0];
                MonthTfld.Text = dateText[1];
                YearTfld.Text = dateText[2];
            }
            catch (Exception ex) {
                Console.WriteLine(ex.Message);
            }
            phoneNumTfld.Text = biodata.phoneNum;
            if (biodata.sex.ToString() == "Male")
            {
                radioButton1.Checked = true;
            }
            else {
                radioButton2.Checked = true;
            }
            if (parents.Count > 0)
            {
                fathersNameTfld.Text = parents.ElementAt(0).names;
                fathersTribeTfld.Text = parents.ElementAt(0).tribe;
                FatherEducation.Text = parents.ElementAt(0).occupation;
            }
            if (parents.Count == 2)
            {

                mothersNameTfld.Text = parents.ElementAt(1).names;
                mothersTribeTfld.Text = parents.ElementAt(1).tribe;
                MotherEducation.Text = parents.ElementAt(1).occupation;
            }

            villageTFld.Text = address.ElementAt(0).village;
            countyTfld.Text = address.ElementAt(0).county;
            districtTfld.Text = address.ElementAt(0).district;
            phoneNumTfld.Text = address.ElementAt(0).telcaretaker;
            telSelfTfld.Text = address.ElementAt(0).telself;
            CareTakerslabel.Text = address.ElementAt(0).personWhoCares;
            submitBtn.Visible=false;
            string addr = address.ElementAt(0).personWhoCares;
            bool others = true;
            foreach(TreeNode pnode in CaringPersonTree.Nodes){
                foreach (TreeNode cnode in pnode.Nodes) { 
                if(cnode.Text.Equals(addr.Trim())){
                    cnode.Checked = true;
                    others = false;
                }
                }
            }
            //if no value found, check on other specify
            if (others == true)
            {
                OthersPersonTfld.Visible = true;
                OthersPersonTfld.Text = addr;
            }
        }

        private void CaringPersonTree_AfterSelect(object sender, TreeViewEventArgs e)
        {
            
            if (CaringPersonTree.SelectedNode.Name.Equals("Node2"))
            {
                OthersPersonTfld.Visible = true;
            }
            else
            { 
               OthersPersonTfld.Visible =false ;
            }
            

        }
        private void CaringPersonTree_AfterCheck(object sender, TreeViewEventArgs e) {
            if (e.Node.Name.Equals("Node2")&& e.Node.Checked)
            {
                OthersPersonTfld.Visible = true;
            }
            else
            {
                OthersPersonTfld.Visible = false;
            }
        }

        private void label22_Click(object sender, EventArgs e)
        {

        }

        private void updatebtn_Click(object sender, EventArgs e)
        {
            string names = firstNameTFld.Text + " " + middleNameTfld.Text + " " + surnameTfld.Text;
            string dob = DayTfld.Text + "-" + MonthTfld.Text + "-" + YearTfld.Text;
            if (radioButton2.Checked == false && radioButton1.Checked == false)
            {
                MessageBox.Show(this, " Select the Sex ");
            }

            Patient patient = biodata;
            patient.names = names;
            patient.hospNum = hospitalNumTfld.Text;
            patient.clinicNum = clinicNumTfld.Text;
            patient.dateOfBirth = dob;
            patient.phoneNum = phoneNumTfld.Text;
            patient.sex = radioButton2.Checked ? "Male" : "Female";
            Parents parents = biodata.parentses.ElementAt(0);
            parents.names = fathersNameTfld.Text;
            parents.description = "father";
            parents.tribe = fathersTribeTfld.Text;
            parents.occupation = FatherEducation.Text;
            patient.parentses.Add(parents);
            parents.patient = patient;

            Parents mother = biodata.parentses.ElementAt(1);
            mother.names = mothersNameTfld.Text;
            mother.description = "mother";
            mother.tribe = mothersTribeTfld.Text;
            mother.occupation = MotherEducation.Text;
            patient.parentses.Add(mother);
            mother.patient = patient;

            Addres patientAddr = new Addres();
            patientAddr.village = villageTFld.Text;
            patientAddr.county = countyTfld.Text;
            patientAddr.district = districtTfld.Text;
            patientAddr.telcaretaker = phoneNumTfld.Text;
            patientAddr.telself = telSelfTfld.Text;
            patientAddr.personWhoCares = getCaringPerson().ToString().Trim();
            patient.addreses.Add(patientAddr);
            patientAddr.patient = patient;
                    
            if (PatientRegistration.updatePatient(patient))
            {
                MessageBox.Show(this, "Successfully added");
            }
            else
            {
                MessageBox.Show(this, "Patient already exists");
            }
                       
           
        }

        public StringBuilder getCaringPerson()
        {
            StringBuilder caringPersons = new StringBuilder();
            foreach (TreeNode pnode in CaringPersonTree.Nodes)
            {
                foreach (TreeNode cnode in pnode.Nodes)
                {
                    if (cnode.Checked && !cnode.Name.Equals("Node2"))
                    {
                        caringPersons.Append(cnode.Text);
                        Console.WriteLine(cnode.Text);
                    }
                }
            }
            return caringPersons;
        }

        
    }
}
